

function r = getLocalOrientation(im,pos)

%% check for exceptions
if length(pos) == 1
    [Y,X] = ind2sub(size(im),pos);
else
    Y = pos(1); X = pos(2);         %position should be given in [row, col] format
end

if im(Y,X) ~= 1
    disp('indicated position has to be a positive pixel')
    r = nan;
    return;
end

%omit points too close to the edges of the image:
edgethresh = 30;
if X < edgethresh || Y < edgethresh || X>(size(im,2)-edgethresh) || Y>(size(im,1)-edgethresh)
    r = nan;
    return;
end

%% set parameter values
binsz = 6;      %sets +/- size of box to use for defining local orientation (in pixels)


subim = im((Y-binsz):(Y+binsz),(X-binsz):(X+binsz));
temp = regionprops(subim,'Orientation');
r = temp.Orientation;